package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.entity.Content;
import com.czz.manage.entity.Question;
import com.czz.manage.entity.QuestionComment;
import com.czz.manage.mapper.ContentMapper;
import com.czz.manage.mapper.QuestionCommentMapper;
import com.czz.manage.mapper.QuestionMapper;
import com.czz.manage.query.QuestionQuery;
import com.czz.manage.service.QuestionService;
import com.czz.manage.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @ClassName: QuestionServiceImpl
 * @Author Czz
 * @Date 2020/4/27
 * @Time 18:30
 **/
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ContentMapper contentMapper;

    @Autowired
    QuestionCommentMapper commentMapper;

    @Override
    public void getQuestionPageList(Page<Question> pageParam, QuestionQuery query) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");

        if (!ObjectUtils.isEmpty(query)){
            questionMapper.selectPage(pageParam,queryWrapper);
        }

        String title = query.getTitle();
        String tagId = query.getTagId();
        String begin = query.getBegin();
        String end = query.getEnd();

        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("question_title",title);
        }

        if (!StringUtils.isEmpty(tagId)){
            queryWrapper.like("question_tag",tagId);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        questionMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    @Transactional
    public void deleteQuestionById(String id) {
        //删除question表中信息
        questionMapper.deleteById(id);

        //删除content表中信息
        QueryWrapper<Content> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.eq("question_id",id);
        contentMapper.delete(contentQueryWrapper);

        //删除对应的回复
        QueryWrapper<QuestionComment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("question_id",id);
        commentMapper.delete(commentQueryWrapper);
    }

    @Override
    public QuestionVo getQuestionById(String id) {
        QuestionVo questionVo = new QuestionVo();

        Question question = questionMapper.selectById(id);
        QueryWrapper<Content> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.eq("question_id",id);
        Content content = contentMapper.selectOne(contentQueryWrapper);

        questionVo.setQuestionId(id);
        questionVo.setContent(content.getContent());
        questionVo.setContentId(content.getContentId());
        questionVo.setTitle(question.getTitle());
        questionVo.setTag(question.getTag());

        return questionVo;
    }

    @Override
    @Transactional
    public void updateQuestionById(String id, QuestionVo questionVo) {
        //更新question表
        Question question = new Question();
        question.setQuestionId(id);
        question.setTitle(questionVo.getTitle());
        question.setTag(questionVo.getTag());
        questionMapper.updateById(question);

        //更新问答content表
        Content content = new Content();
        content.setContentId(questionVo.getContentId());
        content.setContent(questionVo.getContent());
        contentMapper.updateById(content);
    }
}
